<?php

namespace app\controller\api;

use app\model\PromotionModel;
use app\model\PromotionItemModel;
use support\Request;
use app\exception\BusinessException;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("促销活动接口")
 * @Apidoc\Group("api")
 */
class PromotionController
{
    /**
     * @Apidoc\Title("获取促销活动列表")
     * @Apidoc\Method("GET")
     * @Apidoc\Url("/api/promotion/list")
     * @Apidoc\Param("page", type="int", default=1, desc="页码")
     * @Apidoc\Param("page_size", type="int", default=10, desc="每页数量")
     * @Apidoc\Param("type", type="int", desc="活动类型")
     */
    public function list(Request $request)
    {
        $page = $request->get('page', 1);
        $pageSize = $request->get('page_size', 10);
        $type = $request->get('type');
        
        $query = PromotionModel::where('status', 1)
            ->where('start_time', '<=', date('Y-m-d H:i:s'))
            ->where('end_time', '>=', date('Y-m-d H:i:s'));
            
        if ($type) {
            $query->where('type', $type);
        }
        
        $promotions = $query->orderBy('id', 'desc')
            ->paginate($pageSize, ['*'], 'page', $page);
            
        return json(['code' => 0, 'msg' => 'success', 'data' => $promotions]);
    }
    
    /**
     * @Apidoc\Title("获取促销活动详情")
     * @Apidoc\Method("GET")
     * @Apidoc\Url("/api/promotion/detail/{id}")
     * @Apidoc\Param("id", type="int", require=true, desc="促销活动ID")
     */
    public function detail($id)
    {
        $promotion = PromotionModel::where('status', 1)
            ->where('start_time', '<=', date('Y-m-d H:i:s'))
            ->where('end_time', '>=', date('Y-m-d H:i:s'))
            ->find($id);
            
        if (!$promotion) {
            throw new BusinessException('促销活动不存在或已结束');
        }
        
        return json(['code' => 0, 'msg' => 'success', 'data' => $promotion]);
    }
    
    /**
     * @Apidoc\Title("获取促销商品列表")
     * @Apidoc\Method("GET")
     * @Apidoc\Url("/api/promotion/product/list")
     * @Apidoc\Param("promotion_id", type="int", desc="促销活动ID")
     * @Apidoc\Param("page", type="int", default=1, desc="页码")
     * @Apidoc\Param("page_size", type="int", default=10, desc="每页数量")
     */
    public function productList(Request $request)
    {
        $promotionId = $request->get('promotion_id');
        $page = $request->get('page', 1);
        $pageSize = $request->get('page_size', 10);
        
        $query = PromotionItemModel::with(['product', 'sku'])
            ->whereHas('promotion', function ($query) {
                $query->where('status', 1)
                    ->where('start_time', '<=', date('Y-m-d H:i:s'))
                    ->where('end_time', '>=', date('Y-m-d H:i:s'));
            });
            
        if ($promotionId) {
            $query->where('promotion_id', $promotionId);
        }
        
        $items = $query->orderBy('sort', 'asc')
            ->orderBy('id', 'desc')
            ->paginate($pageSize, ['*'], 'page', $page);
            
        return json(['code' => 0, 'msg' => 'success', 'data' => $items]);
    }
    
    /**
     * @Apidoc\Title("获取促销商品详情")
     * @Apidoc\Method("GET")
     * @Apidoc\Url("/api/promotion/product/detail/{id}")
     * @Apidoc\Param("id", type="int", require=true, desc="促销商品ID")
     */
    public function productDetail($id)
    {
        $item = PromotionItemModel::with(['product', 'sku', 'promotion'])
            ->whereHas('promotion', function ($query) {
                $query->where('status', 1)
                    ->where('start_time', '<=', date('Y-m-d H:i:s'))
                    ->where('end_time', '>=', date('Y-m-d H:i:s'));
            })
            ->find($id);
            
        if (!$item) {
            throw new BusinessException('促销商品不存在或已结束');
        }
        
        return json(['code' => 0, 'msg' => 'success', 'data' => $item]);
    }
} 